﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HFController;

namespace HFWebManage.Ask
{
    public partial class Ask : HFController.BagePages
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //判断管理员有没有登录，如果没登录，踢出
            this.checkAdminIsLogin();

             //是否首次加载
            if (!Page.IsPostBack)
            {
                //绑定省份城市
                DropCityID.Items.Insert(0, new ListItem("--请选择--", "0"));
                this.bindProvinceID();
                //绑定法律事务
                DropSmallClassID.Items.Insert(0, new ListItem("--请选择--", "0"));
                this.bindLegalAffairsBiglClass();

                //获取Action参数，判断是修改还是删除
                string Action = this.GetQuery("action");

                switch (Action)
                {
                    case "modify":
                        //表示修改
                        this.bindmodel();
                        break;
                    case "delete":
                        //表示删除
                        int ID = int.Parse(this.GetQuery("ID"));
                        AskController.getController().doDelegeAsk(ID);
                        break;
                }
            }
        }

        /// <summary>
        /// 绑定省份
        /// </summary>
        public void bindProvinceID()
        {
            DropProvinceID.DataSource = CityController.getController().getProvinceList(100);
            DropProvinceID.DataValueField = "ID";
            DropProvinceID.DataTextField = "ProvinceName";
            DropProvinceID.DataBind();
            DropProvinceID.Items.Insert(0, new ListItem("--请选择--", "0"));
        }

        /// <summary>
        /// 省份选定出发的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void DropProvinceID_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.bindCityID();
        }

        /// <summary>
        /// 绑定城市
        /// </summary>
        public void bindCityID()
        {
            DropCityID.DataSource = CityController.getController().getCityList(100, DropProvinceID.SelectedValue);
            DropCityID.DataValueField = "ID";
            DropCityID.DataTextField = "CityName";
            DropCityID.DataBind();
            DropCityID.Items.Insert(0, new ListItem("--请选择--", "0"));
        }

        /// <summary>
        /// 根据法律事务的ID，绑定法律事务大分类
        /// </summary>
        /// <param name="LegalAffairsID"></param>
        private void bindLegalAffairsBiglClass()
        {
            dropClassID.DataSource = LegalAffairsController.getController().getLegalAffairsBigClass(100);
            dropClassID.DataValueField = "ID";
            dropClassID.DataTextField = "ClassName"; 
            dropClassID.DataBind(); //执行绑定
            dropClassID.Items.Insert(0, new ListItem("--请选择--", "0"));
        }
        /// <summary>
        /// 法律事务分类选择是出发
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void dropClassID_SelectedIndexChanged(object sender, EventArgs e)
        {
            /*
             *  这里错了，你应该传个大类的ID 吧，你传的是小类ID
            this.bindLegalAffairsSmallClass(int.Parse(this.DropSmallClassID.SelectedValue));
             * */
            this.bindLegalAffairsSmallClass(int.Parse(this.dropClassID.SelectedValue));
            
        }

        /// <summary>
        /// 根据法律事务的ID，绑定法律事务小分类
        /// </summary>
        /// <param name="LegalAffairsID"></param>
        private void bindLegalAffairsSmallClass(int LegalAffairsBigClassID)
        {
            DropSmallClassID.DataSource = LegalAffairsController.getController().getLegalAffairsSmallClass(2000, "BigClassName=" + LegalAffairsBigClassID.ToString());
            DropSmallClassID.DataValueField = "ID";
            DropSmallClassID.DataTextField = "SmallClassName"; 
            DropSmallClassID.DataBind(); //执行绑定
            DropSmallClassID.Items.Insert(0, new ListItem("--请选择--", "0"));
        }

        private void bindmodel()
        {
            int ID = int.Parse(this.GetQuery("ID"));
            //得到这条记录
            hf.Model.Ask model = AskController.getController().getAskModel(ID);
            //输入框赋值
            tbTitle.Text = model.Title;
            dropClassID.SelectedValue = model.ClassID.ToString();
            DropSmallClassID.SelectedValue = model.SmallClassID.ToString();
            tbUserID.Text = model.UserID.ToString();
            DropProvinceID.SelectedValue = model.ProvinceID.ToString();
            DropCityID.SelectedValue = model.CityID.ToString();
            tbPoints.Text = model.Points.ToString();
            tbAnswerCount.Text = model.AnswerCount.ToString();
            tbContent.Text = model.Content;
            rblisIndex.SelectedValue = model.isIndex.ToString();

            //隐藏域ID赋值
            IDS.Value = model.ID.ToString();
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            //获取Action参数，判断是修改还是保存
            string Action = this.GetQuery("action");
            if (Action == "modify")
            {
                //修改记录，刚才bindmodel()方法里，赋值的那两个
                AskController.getController().doModifyAsk(int.Parse(IDS.Value),
                    tbTitle.Text,
                    int.Parse(dropClassID.SelectedValue),
                    int.Parse(DropSmallClassID.SelectedValue),
                    tbContent.Text,
                    int.Parse(tbAnswerCount.Text),
                    int.Parse(tbUserID.Text),
                    int.Parse(tbPoints.Text),
                    int.Parse(DropProvinceID.SelectedValue),
                    int.Parse(DropCityID.SelectedValue),
                    int.Parse(tbStatusID.Text),
                    int.Parse(rblisIndex.SelectedValue)
                    );
            }
            else
            {
                AskController.getController().doAddAsk(tbTitle.Text,
                    int.Parse(dropClassID.SelectedValue),
                    int.Parse(DropSmallClassID.SelectedValue),
                    tbContent.Text,
                    int.Parse(tbAnswerCount.Text),
                    int.Parse(tbUserID.Text),
                    int.Parse(tbPoints.Text),
                    int.Parse(DropProvinceID.SelectedValue),
                    int.Parse(DropCityID.SelectedValue),
                    int.Parse(tbStatusID.Text),
                    int.Parse(rblisIndex.SelectedValue)
                    );
            }
        }
    }
}